package database.access.layer;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import domain.Movie;
import domain.User;

public class MovieDal {
	protected ConnectionManager mgr = null;
	protected Connection con = null;
	
	public MovieDal(){
	}
	
	public void saveMovie(Movie movie) throws SQLException{
		this.mgr = ConnectionManager.getInstance();
		this.con = mgr.getConnection();
		Statement st = con.createStatement() ;
		st.executeUpdate("insert into MOVIE (id, name , description)" +
				"values ("+ movie.getMovieDescription().getMovieId() + ", '" + movie.getMovieDescription().getName() + "', '"+movie.getMovieDescription().getDescription()+"')");
		
		st.close();
		mgr.closeConnection(con);
	}
	
	public Movie findMovieByName(String movieName) throws SQLException{
		this.mgr = ConnectionManager.getInstance();
		this.con = mgr.getConnection();
		Statement st = con.createStatement();
		ResultSet rs = st.executeQuery("select * from MOVIE where name = '"+movieName+"'");
		rs.next();
		Movie movie = new Movie(Integer.parseInt(rs.getString(1)), rs.getString(2), rs.getString(3));
		rs.close();
		st.close();
		mgr.closeConnection(con);
		return movie;
	}
	
	public Movie findMovieById(int movieId) throws SQLException{
		this.mgr = ConnectionManager.getInstance();
		this.con = mgr.getConnection();
		Statement st = con.createStatement();
		ResultSet rs = st.executeQuery("select * from MOVIE where MOVIEID = '"+movieId+"'");
		rs.next();
		Movie movie = new Movie(Integer.parseInt(rs.getString(1)), rs.getString(2), rs.getString(3));
		rs.close();
		st.close();
		mgr.closeConnection(con);
		return movie;
		
	}
	
}
